package cn.mdmbct.douyin.spring.example.vo;

import cn.mdmbct.douyin.spring.example.enums.ResultCode;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import java.io.Serializable;


@Data
@ApiModel(value = "返回实体")
public class Result<T> implements Serializable {

    private static final long serialVersionUID = 2934276400746920480L;

    @ApiModelProperty(value = "是否成功")
    private boolean success;
    @ApiModelProperty(value = "返回码")
    private Integer code;
    @ApiModelProperty(value = "返回信息")
    private String message;
    @ApiModelProperty(value = "返回对象")
    private T data;

    public Result(ResultCode code) {
        this.success = code.isSuccess();
        this.code = code.getCode();
        this.message = code.getMessage();
    }

    public Result(ResultCode code, T data) {
        this.success = code.isSuccess();
        this.code = code.getCode();
        this.message = code.getMessage();
        this.data = data;
    }

    public Result(Integer code, String message, boolean success) {
        this.code = code;
        this.message = message;
        this.success = success;
    }

    public Result(boolean success, Integer code, String message, T data) {
        this.success = success;
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public static <T> Result<T> success() {
        return new Result<>(ResultCode.SUCCESS);
    }

    public static <T> Result<T> success(T data) {
        return new Result<>(ResultCode.SUCCESS, data);
    }

    public static <T> Result<T> error() {
        return new Result<>(ResultCode.SERVER_ERROR);
    }

    public static Result<StackTraceElement[]> error(String errorMsg, StackTraceElement[] stackTrace) {
        return new Result<>(false, 500, errorMsg, stackTrace);
    }

    public static <T> Result<T> error(String errorMsg) {
        return new Result<>(false, 500, errorMsg, null);
    }


    public static <T> Result<T> fail() {
        return new Result<>(ResultCode.FAIL);
    }

    public static <T> Result<T> fail(String message, T data) {
        return new Result<>(false, 400, message, data);
    }

    public static <T> Result<T> fail(String message) {
        return new Result<>(false, 400, message, null);
    }

}
